package com.yra.controller.manager;

import com.yra.common.IndexConfigTypeEnum;
import com.yra.service.IndexConfigService;
import com.yra.util.PageQueryUtil;
import com.yra.util.Result;
import com.yra.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: yrq
 * @Date: Create in 11:34 2019/12/17
 */
@Controller
@RequestMapping("/manager")
public class IndexConfigController {
    @Autowired
    private IndexConfigService indexConfigService;

    @RequestMapping(value = "/indexConfigs")
    public ModelAndView goToIndexConfig(HttpServletRequest request, @RequestParam("configType") int configType){
        IndexConfigTypeEnum indexConfigTypeEnum = IndexConfigTypeEnum.getIndexConfigTypeEnumByType(configType);
        request.setAttribute("path", indexConfigTypeEnum.getName());
        request.setAttribute("configType", configType);
        return new ModelAndView("/admin/newbee_mall_index_config");
    }

    @RequestMapping(value = "/indexConfigs/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Result> list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResultGenerator.genFailResult("参数异常！"));
        }
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        return ResponseEntity.status(HttpStatus.OK).body(ResultGenerator.genSuccessResult(indexConfigService.getConfigsPage(pageQueryUtil)));
    }

    @RequestMapping(value = "/indexConfigs/save",method = RequestMethod.POST)
    public ResponseEntity<String> insertConfig(@RequestBody Map<String,Object> params){
        System.out.println(params);
        if(indexConfigService.insertConfig(params)){
            return ResponseEntity.status(HttpStatus.OK).body("添加");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @RequestMapping(value = "/indexConfigs/update",method = RequestMethod.POST)
    public ResponseEntity<String> updateConfig(@RequestBody Map<String,Object> params){
        System.out.println(params);
        if (indexConfigService.updateConfig(params)){
            return ResponseEntity.status(HttpStatus.OK).body("修改");
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @RequestMapping(value = "/indexConfigs/delete",method = RequestMethod.POST)
    public ResponseEntity<Boolean> deleteConfig(@RequestBody int[] ids){
        System.out.println(ids[0]);

        return ResponseEntity.status(HttpStatus.OK).body(indexConfigService.deleteConfigs(ids));
    }
}
